import { h, Component } from 'preact';
import s from './style';
import classNames from 'classnames';
import gift from '~/assets/gift.png';
import goldlight from '~/assets/goldlight.png';
import { connect } from 'preact-redux';
import {grabGiftAction} from '~/actions/entrance';
class Gift extends Component {
	constructor() {
		super();
	}
	onClick = () => {
		const { entrance, dispatch } = this.props;
		const { recommend, user, subscribe } = entrance || {};
		const { userPhone } = user || {};
		if (subscribe === 0) {
			dispatch({type:'openAttention'});
		} else if (!userPhone) {
			dispatch({type:'openPhone'});
		} else {
			dispatch(grabGiftAction());
		}
	}
	render() {
		const leftClass = classNames("al-c", s.leftText);
		const { onClick } = this;
		const { entrance } = this.props;
		const { recommend } = entrance || {};
		const { drawTimes, drawCost } = recommend || {};
		const drawLeave = drawTimes - drawCost >= 0 ? drawTimes - drawCost : 0;
		return (
			<div className={s.giftBox}>
				<img src={goldlight} className={s.goldlight} />
				<img src={gift} onClick={onClick} className={s.gift}/>
				<div className={leftClass}>剩余<span>{drawLeave}</span>次</div>
			</div>
		);
	}
}

export default connect(state => (state))(Gift);
